<?php

class AppController extends Controller {
    var $components = array ('Auth', 'Session');

    function beforeFilter() {
        $this->Auth->allow('index', 'view', 'display');
        $this->Auth->authError = 'Please login to view that page.';
        $this->Auth->loginError = 'Incorrect username/password combination.';
        $this->Auth->loginRedirect = array('controller' => 'pages', 'action' => 'display', 'home');
        $this->Auth->logoutRedirect = array('controller' => 'pages', 'action' => 'display', 'home');

        $this->set('admin', $this->_isAdmin());
        $this->set('logged_in', $this->_loggedIn());
        $this->set('users_username', $this->_usersUsername());
        $this->set('users_id', $this->_usersId());

    }

    function _isAdmin() {
        $admin = FALSE;
        if ($this->Auth->user('role') == 'admin') {
            $admin = TRUE;
        }
        return $admin;
    }

    function _loggedIn() {
        $logged_in = FALSE;
        if ($this->Auth->user()) {
            $logged_in = TRUE;
        }
        return $logged_in;
    }

    function _usersUsername() {
        $users_username = NULL;
        if ($this->Auth->user()) {
            $users_username = $this->Auth->user('username');
        }
        return $users_username;
    }

    function _usersId() {
        $users_id = NULL;
        if ($this->Auth->user()) {
            $users_id = $this->Auth->user('id');
        }
        return $users_id;
    }


}
